package vn.com.vega.chacha.v2;

import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.Asynctask.LoadPlaylistCreateByUser;
import vn.com.vega.chacha.v2.Asynctask.LoadPlaylistCreateByUser.LoadUserPlaylistListener;
import vn.com.vega.music.database.DataStore;
import vn.com.vega.music.objects.Playlist;
import vn.com.vega.music.player.ChaChaProgressDialog;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.utils.PlaylistUtil;
import vn.com.vega.music.utils.Util;
import vn.com.vega.music.view.adapter.ListPlaylistPersonalMusicAdapter;
import vn.com.vega.music.view.adapter.PersonalMusicGridAdapter;
import vn.com.vega.music.view.custom.BottomPlayerView;
import vn.com.vega.music.view.custom.TopTaskbarView;
import vn.com.vega.music.view.custom.BottomPlayerView.ShowHideBottomPlayerListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.provider.Contacts.Intents;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PersonalMusic extends ActivityBase implements OnClickListener {
	private TopTaskbarView mTopTaskbarView;
	private GridView mGridView;
	private PersonalMusicGridAdapter mGridAdapter;
	private View mShowHidePlaylist;
	private ImageView mArrowShowhidePlaylist;
	private TextView mMyPlaylistLabel;
	private ImageButton mAddPlaylistBtn;
	private List<Playlist> mListUserPlaylist;
	private ListView mListPlaylistLv;
	private ListPlaylistPersonalMusicAdapter mListPlaylistAdapter;
	private Appinstant mApp;
	private LoadPlaylistCreateByUser mLoadUserPlaylist;
	private BottomPlayerView mBottomPlayer;
	private View mPlayerViewReplace;
	private ChaChaProgressDialog progress;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.personal_music_screen);
		mApp = (Appinstant) getApplication();
		mTopTaskbarView = (TopTaskbarView) findViewById(R.id.topTaskbarView_personal_music);
		mTopTaskbarView.showBackButton();
		mGridView = (GridView) findViewById(R.id.gridview_list_item_personal_music);
		mGridAdapter = new PersonalMusicGridAdapter(this);
		mGridView.setAdapter(mGridAdapter);
		mGridView.setOnItemClickListener(onGridItemClickListener);

		mShowHidePlaylist = findViewById(R.id.my_playlist);
		mArrowShowhidePlaylist = (ImageView) findViewById(R.id.imv_icon_show_hide_playlist);
		mMyPlaylistLabel = (TextView) findViewById(R.id.tv_playlist_text);
		mMyPlaylistLabel.setText("Playlist cá nhân");
		mAddPlaylistBtn = (ImageButton) findViewById(R.id.bt_add_playlist);
		mAddPlaylistBtn.setOnClickListener(this);
		mShowHidePlaylist.setOnClickListener(this);

		mListPlaylistLv = (ListView) findViewById(R.id.lv_list_user_playlist);
		mListPlaylistLv.setOnItemClickListener(onPlaylistClickListener);
		
		mPlayerViewReplace = findViewById(R.id.view_replace_bottom_player);
		mBottomPlayer = (BottomPlayerView) findViewById(R.id.bottomPlayerView_personal_music);
		mBottomPlayer.setShowHideListener(showHideBottomPlayerListener);
		progress = new ChaChaProgressDialog(this);
		
		loadUserPlaylist();
		IntentFilter intenFillter = new IntentFilter(Const.LIST_PLAYLIST_CHANGE);
		registerReceiver(receiver, intenFillter);
	}

	ShowHideBottomPlayerListener showHideBottomPlayerListener = new ShowHideBottomPlayerListener() {

		@Override
		public void onShowBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideBottomPlayer() {
			// TODO Auto-generated method stub
			mPlayerViewReplace.setVisibility(View.GONE);
		}
	};

	AdapterView.OnItemClickListener onGridItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			switch (arg2) {
			case 0:
				gotoLocalSong();
				break;
			case 1:
				gotoLikedSong();
				break;
			case 2:
				gotoSongListenRecently();
				break;
			case 3:
				gotoSongDownloaded();
				break;

			default:
				break;
			}
		}
	};

	private void gotoLocalSong() {
		Intent intent = new Intent(this, LocalSongScreen.class);
		startActivity(intent);
	}

	private void gotoLikedSong() {
//		DataStore dataStore = DataStore.getInstance();
//		Playlist favorite = dataStore
//				.getSpecialPlaylistByType(Playlist.TYPE_FAVORITE);
//		if (favorite != null) {
//			showPlaylist(favorite);
//		} else {
//			showToast("Chưa có danh sách nhạc yêu thích");
//		}
		Intent like = new Intent(this, LikedScreen.class);
		like.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(like);
	}

	private void gotoSongListenRecently() {
		Intent intent = new Intent(this, SongListenRecently.class);
		startActivity(intent);
	}

	private void gotoSongDownloaded() {
		Intent intent = new Intent(this, DownloadedScreen.class);
		startActivity(intent);
	}

	private void showPlaylist(Playlist playlist) {
		if (playlist.total_song == 0) {
			showToast("Playlist trống!");
			return;
		}
		String playlistName = PersonalMusic.class.getName();
		mApp.tranportPlaylist.put(playlistName, playlist);
		Intent intent = new Intent(PersonalMusic.this, PlaylistDetail.class);
		intent.putExtra(PlaylistDetail.KEY_PLAYLIST_NAME, playlistName);
		startActivity(intent);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(receiver);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.my_playlist:
			if (mArrowShowhidePlaylist.isSelected()) {
				showListPlaylist(false);
			} else {
				showListPlaylist(true);
			}
			break;
		case R.id.bt_add_playlist:
			PlaylistUtil.createNewPlaylist(this, null);
			break;
		default:
			break;
		}
	}

	private void showListPlaylist(boolean show) {
		if (show) {
			mArrowShowhidePlaylist.setSelected(true);
			mListPlaylistLv.setVisibility(View.VISIBLE);
			mListPlaylistLv.setSelectionAfterHeaderView();
		} else {
			mArrowShowhidePlaylist.setSelected(false);
			mListPlaylistLv.setVisibility(View.GONE);
		}
	}

	AdapterView.OnItemClickListener onPlaylistClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Playlist playlist = (Playlist) mListPlaylistAdapter.getItem(arg2);
//			playlist.type = Playlist.TYPE_USER_CREATED;
			showPlaylist(playlist);
		}
	};

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (Const.LIST_PLAYLIST_CHANGE.equalsIgnoreCase(action)) {
				loadUserPlaylist();
			}
		}
	};

	private void loadUserPlaylist() {
		progress.setMessage("Tải danh sách playlist...");
		LoadUserPlaylistListener loadPlaylistListener = new LoadUserPlaylistListener() {

			@Override
			public void startLoadPlaylist() {
				// TODO Auto-generated method stub
				progress.show();
			}

			@Override
			public void loadPlaylistSuccess(
					List<Playlist> listUserCreatePlaylist) {
				// TODO Auto-generated method stub
				mListUserPlaylist = listUserCreatePlaylist;
				mMyPlaylistLabel.setText("Playlist cá nhân ("
						+ mListUserPlaylist.size() + ")");
				mListPlaylistAdapter = new ListPlaylistPersonalMusicAdapter(
						PersonalMusic.this, mListUserPlaylist);
				mListPlaylistLv.setAdapter(mListPlaylistAdapter);
				showListPlaylist(true);
			}

			@Override
			public void loadPlaylistError(String errorMessage) {
				// TODO Auto-generated method stub
				if (Util.checkStringIsemptyOrNull(errorMessage)) {
					showToast("Tải danh sách playlist lỗi!");
				} else {
					showToast(errorMessage);
				}
			}

			@Override
			public void finishLoadPlaylist() {
				// TODO Auto-generated method stub
				progress.dismiss();
			}
		};
		if (mLoadUserPlaylist != null) {
			mLoadUserPlaylist.cancel(true);
		}
		mLoadUserPlaylist = new LoadPlaylistCreateByUser(this,
				loadPlaylistListener);
		mLoadUserPlaylist.execute();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mBottomPlayer.startUpdate();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		mBottomPlayer.stopUpdate();
	}
}
